package com.hhp.cloud.shop.admin.controller;

import java.util.Arrays;
import java.util.Optional;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hhp.cloud.shop.admin.pojo.entity.PermissionEntity;
import com.hhp.cloud.shop.admin.service.PermissionService;

import com.hhp.cloud.shop.common.core.result.R;



/**
 * 权限表
 *
 * @author hhp
 * @email 2437579794@qq.com
 * @date 2021-11-23 10:25:33
 */
@RestController
@RequestMapping("/api/v1/admin/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 列表
     */
    @ApiOperation(value = "权限分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "name", value = "权限名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "menuId", value = "菜单id", required = false, paramType = "query", dataType = "Long"),
    })
    @GetMapping
    public R list(Integer page, Integer pageSize,
                  @RequestParam(value = "name", required = false) String name,
                  @RequestParam(value = "menuId", required = false) Long menuId){
        return R.ok().put("data", permissionService.page(
                new Page<PermissionEntity>(page, pageSize),
                new LambdaQueryWrapper<PermissionEntity>()
                        .eq(Optional.ofNullable(menuId).isPresent(), PermissionEntity::getMenuId, menuId)
                        /*.eq(PermissionEntity::getMenuId, menuId)*/
                        .like(StrUtil.isNotBlank(name), PermissionEntity::getName, name)).getRecords());
    }


    /**
     * 信息
     */
    @ApiOperation(value = "权限详情")
    @ApiImplicitParam(name = "id", value = "权限id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public R info(@PathVariable("id") Long id){
        return R.ok().setData(permissionService.getById(id));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增权限")
    @PostMapping
    public R save(@RequestBody PermissionEntity permission){
		permissionService.save(permission);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改权限")
    @PutMapping("/{id}")
    public R update(@PathVariable("id") Long id, @RequestBody PermissionEntity permission){
		permissionService.updateById(permission);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除权限")
    @DeleteMapping
    public R delete(@RequestBody Long[] ids){
		permissionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
